package cn.jingzhuan.fm.utils

import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.support.v4.content.ContextCompat
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import cn.jingzhuan.fm.R
import cn.jingzhuan.fm.databinding.ItemFmShareBinding
import cn.jingzhuan.fm.pojo.ShareItem
import cn.jingzhuan.fm.ui.adapter.BaseBindingRecyclerAdapter
import cn.jingzhuan.fm.ui.adapter.BindingRecyclerViewHolder
import com.bilibili.socialize.share.core.BiliShare
import com.bilibili.socialize.share.core.BiliShareConfiguration
import com.bilibili.socialize.share.core.SocializeListeners.ShareListener
import com.bilibili.socialize.share.core.SocializeMedia
import com.bilibili.socialize.share.core.shareparam.ShareParamWebPage


/**
 * Created by bilue on 17/6/5.
 * bilibili share 工具类
 */
class ShareUtil {

  companion object {
    private var mShareClient: BiliShare? = null
    const val QQ_ID = "1103195173"
    const val WECHAT_ID = "wx4260e9d614f6335d"
    const val SINA_KEY = "701185282"
    const val SINA_REDIRECTURL = "https://api.weibo.com/oauth2/default.html"


    fun shareUrl(activity: Activity, title: String, content: String, target: String,
        listener: ShareListener) {
      initShareClient(activity)
      val paramWebPage = ShareParamWebPage(title, content, target)

//      val image = ShareImage(R.drawable.icon_share_default)
//      paramWebPage.thumb = image
      showShareDialog(activity, object : OnMediaSelectedListener {
        override fun onMediaSelected(type: SocializeMedia) {
          mShareClient?.share(activity, type, paramWebPage, listener)
        }
      })
    }

    private fun showShareDialog(activity: Activity, listener: OnMediaSelectedListener) {
      val dialog = Dialog(activity, R.style.share_dialog_theme)
      dialog.setContentView(R.layout.view_fm_share)
      dialog.window!!.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
          ViewGroup.LayoutParams.WRAP_CONTENT)
      dialog.window!!.setGravity(Gravity.BOTTOM)
      dialog.window!!.setWindowAnimations(android.R.style.Animation_InputMethod)
      val rvShare: RecyclerView = dialog.findViewById(R.id.rv_share_item)
      rvShare.layoutManager = GridLayoutManager(activity, 3, GridLayoutManager.VERTICAL, false)
      val adapter = object : BaseBindingRecyclerAdapter<ShareItem>(activity.baseContext, ShareItem.shareItems) {
        override fun getItemLayoutId(viewType: Int): Int = R.layout.item_fm_share

        override fun bindData(holder: BindingRecyclerViewHolder, position: Int, item: ShareItem) {
          val shareItem = ShareItem.shareItems[position]
          var binding = holder.binding as ItemFmShareBinding
          binding.ivIcon.setImageDrawable(
                  ContextCompat.getDrawable(holder.binding.root.context, shareItem.img))

          binding.tvShare.text = shareItem.title
          binding.llShare.setOnClickListener {
            listener.onMediaSelected(item.socializeMedia)
            if (!activity.isFinishing) {
              dialog.dismiss()
            }
          }
        }
      }
      rvShare.adapter = adapter
      dialog.findViewById<View>(R.id.iv_share_cancel).setOnClickListener { dialog.dismiss() }
      dialog.show()
    }


    private fun initShareClient(context: Context) {
      if (mShareClient == null) {
        val configuration = BiliShareConfiguration.Builder(context)
            .sina(SINA_KEY, SINA_REDIRECTURL, null)
            .qq(QQ_ID)
            .weixin(WECHAT_ID)
            .build()
        mShareClient = BiliShare.global()
        mShareClient?.config(configuration) //config只需要配置一次
      }
    }

  }


  interface OnMediaSelectedListener {
    fun onMediaSelected(type: SocializeMedia)
  }

}